const service = require("../../../../service/activity.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    listdata: [],
    pageNum: 1,
    pageSize: 5,
    noData: false,
    noDatas: false
  },
  formatDateTime(date) {
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d + 1) : (d + 1);
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    minute = minute < 10 ? ('0' + minute) : minute;
    var second = date.getSeconds();
    second = second < 10 ? ('0' + second) : second;
    return y + '-' + m + '-' + d + ' ';
  },
  getList(callback) {
    var self = this;
    service.getgroupActivitiesList({
      "groupActivities": {
        "startTime": self.formatDateTime(new Date()) + ' 00:00:00',
        "endTime": self.formatDateTime(new Date()) + ' 23:59:59',
        "customerId": wx.getStorageSync('userInfo').id
      },
      "queryRequest": {
        "pageNum": self.data.pageNum,
        "pageSize": self.data.pageSize
      }
    }, res => {
      //console.log(res)
      for (let i = 0; i < res.data.records.length; i++) {
        const element = res.data.records[i];
        var img = JSON.parse(element.image)
        var url = img.temBanner.split(",");
        element.url = url[0];
      }
      callback(res);
    })
  },
  //    group(array, subGroupLength) {
  //     let index = 0;
  //     let newArray = [];
  //     while(index < array.length) {
  //         newArray.push(array.slice(index, index += subGroupLength));
  //     }
  //     return newArray;
  // },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var self = this;
    // 订单列表
    wx.showLoading({
      title: "加载中...",
    });
    this.getList(function (res) {
      if (res.data.records.length == 0) {
        self.setData({
          noDatas: true,
          noData: false
        });
      } else if (res.data.records.length > 0 && res.data.records.length < 4) {
        self.setData({
          noData: true,
        });
      }
      //  console.log(res.data.records.slice(0,4))
      //  console.log(res.data.records)
      self.setData({
        listdata: res.data.records
      })
      wx.hideLoading();
    });
  },
  todayTo() {
    wx.redirectTo({
      url: "/pages/activity/group/index",
    })
  },
  advanceTo() {
    wx.redirectTo({
      url: "/pages/activity/group/advance/index",
    })
  },
  subscribeTo(e) {
    // wx.updateShareMenu({
    //   withShareTicket: true,
    //   isUpdatableMessage: true,
    //   activityId: '123', // 活动 ID
    //   templateInfo: {
    //     parameterList: [{
    //       name: 'member_count',
    //       value: '1'
    //     }, {
    //       name: 'room_limit',
    //       value: '3'
    //     }]
    //   }
    // })
    let self = this
    let skuName = e.currentTarget.dataset.index.skuTitle
    let groupName = e.currentTarget.dataset.index.groupName
    // let startTime = e.currentTarget.dataset.index.startTime
    let startTime = e.currentTarget.dataset.index.startTime.replace(/\s|-|:/g, "") + '000000'
    let groupId = e.currentTarget.dataset.index.groupId
    let skuId = e.currentTarget.dataset.index.skuId
    console.log(wx.getStorageSync('userInfo'))
    console.log(startTime)

    service.getTemplateId((response) => {
      console.log(1111,response)
      wx.requestSubscribeMessage({
        tmplIds: [response.data],
        success(res) {
          console.log(res);
          if (res[response.data] == "accept") {
            service.groupActivitiesUserAppointment({
              "skuName": skuName,
              "groupName": groupName,
              "customerId": wx.getStorageSync('userInfo').id,
              "startTime": parseInt(startTime),
              "touser": wx.getStorageSync('userInfo').openId,
              "skuId": skuId,
              "groupId": groupId,
              "url": '/pages/activity/group/advance/index'
            }, res => {
              console.log(res)
              if (res.code == "00000") {
                wx.showToast({
                  title: '订阅成功',
                  icon: 'success',
                  duration: 2000
                })
                setTimeout(function () {
                  self.onLoad()
                }, 1000);
              } else {
                wx.showToast({
                  title: '订阅失败',
                  icon: 'none',
                  duration: 2000
                })
              }
            })
          }
        },
        
      })
    })



  },
  gobuy(e) {
    wx.navigateTo({
      url: '/pages/activity/group/groupdetail/index?skuId=' + e.currentTarget.dataset.id + "&unstart=true",
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.onLoad()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading(); //在标题栏中显示加载
    this.setData({
      pageNum: 1,
      noData: false,
    });
    const self = this;
    this.getList(function (res) {
      if (res.data.records.length > 0 && res.data.records.length < 4) {
        self.setData({
          listdata: res.data.records,
          noData: true,
        });
      } else {
        self.setData({
          listdata: res.data.records,
        });
      }
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // 判读数据是否加载完毕
    if (this.data.noData) return;
    // 停止下拉刷新
    wx.stopPullDownRefresh();
    wx.showNavigationBarLoading(); //在标题栏中显示加载
    const listdata = this.data.listdata;
    let pageNum = this.data.pageNum;
    this.setData({
      // 每次下拉 page+1
      pageNum: ++pageNum,
    });
    const self = this;
    self.getList(function (res) {
      if (res.data.records.length == 0) {
        self.setData({
          noData: true,
        });
      } else {
        res.data.records.forEach((item) => {
          listdata.push(item);
        });
        self.setData({
          listdata: listdata
        });
      }
    });
    wx.hideNavigationBarLoading();
    wx.stopPullDownRefresh();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})